/*
 * @Author: wurangkun
 * @Date: 2024-05-21 17:39:19
 * @LastEditTime: 2024-05-22 16:52:33
 * @LastEditors: wurangkun
 * @FilePath: \flutter_example\lib\login\login.dart
 * @Description: 
 */
import 'package:flutter/material.dart';
import 'package:flutter_example/common/UserModel.dart';
import 'package:flutter_example/components/KInput.dart';
import 'package:flutter_example/components/KLabel.dart';
import 'package:flutter_example/models/index.dart';
import 'package:flutter_example/widgets/KButton.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';

class LoginPage extends StatefulWidget {
  const LoginPage({super.key});

  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  late Map<String, String> form = {
    'username': '',
    'password': '',
  };
  UserModel user = UserModel();
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // 页面内容
      body: Padding(
        padding: EdgeInsets.all(40.w),
        child: Flex(
          direction: Axis.vertical,
          children: [
            // 标题
            Text(
              '登录',
              style: TextStyle(fontSize: 48.sp, fontWeight: FontWeight.bold),
            ),
            SizedBox(height: 20.h),
            // 用户名输入框
            KLabel(
              label: '用户名',
              child: KInput(
                hintText: '请输入用户名',
                defaultText: form['username'],
                onChanged: (val) {
                  setState(() {
                    form['username'] = val;
                  });
                },
              ),
            ),
            SizedBox(height: 40.h),
            // 用户名输入框
            KLabel(
              label: '密码',
              child: KInput(
                hintText: '请输入密码',
                defaultText: form['password'],
                onChanged: (val) {
                  setState(() {
                    form['password'] = val;
                  });
                },
              ),
            ),
            KButton(
                child: const Text('登录'),
                onTap: () {
                  print(form);
                  if (form['username'] == '' || form['password'] == '') {
                    showDialog(
                        context: context,
                        builder: (builder) {
                          return AlertDialog(
                            title: const Text('提示'),
                            content: const Text('用户名或密码不能为空'),
                            actions: [
                              TextButton(
                                  onPressed: () {
                                    Navigator.of(context).pop();
                                  },
                                  child: const Text('确定'))
                            ],
                          );
                        });
                  } else {
                    User _user = User.fromJson({
                      "login": "octocat",
                      "avatar_url":
                          "https://github.com/images/error/octocat_happy.gif",
                      "type": "User",
                      "name?": "monalisa octocat",
                      "company?": "GitHub",
                      "blog?": "https://github.com/blog",
                      "location?": "San Francisco",
                      "email?": "octocat@github.com",
                      "hireable?": false,
                      "bio?": "There once was...",
                      "public_repos": 2,
                      "followers": 20,
                      "following": 0,
                      "created_at": "2008-01-14T04:33:35Z",
                      "updated_at": "2008-01-14T04:33:35Z",
                      "total_private_repos": 100,
                      "owned_private_repos": 100
                    });

                    Provider.of<UserModel>(context, listen: false).user = _user;
                    Navigator.pushNamedAndRemoveUntil(
                        context, '/', (route) => false);
                  }
                })
          ],
        ),
      ),
    );
  }
}
